import { ref } from 'vue'
import { defineStore } from 'pinia'
import { getCategoryAPI } from '@/apis/layout'

export const useCategoryStore = defineStore('category', () => {
  // 分类列表数据存储
  const categoryList = ref([])
  // 加载状态
  const loading = ref(false)

  const getCategory = async () => {
    // 检查是否已有数据，避免重复请求
    if (categoryList.value.length > 0) return

    // 添加请求缓存标识，避免并发请求
    if (loading.value) return

    loading.value = true
    try {
      const res = await getCategoryAPI()
      categoryList.value = res.result
    } catch (err) {
      console.error('获取分类数据失败:', err)
    } finally {
      loading.value = false
    }
  }

  return {
    categoryList,
    getCategory
   }
})
